﻿<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" >
		<title>学生端签到页</title>
		<link href="../../css/mui.min.css" rel="stylesheet"/>
		<link rel="stylesheet" type="text/css" href="../../css/base.css"/>
		<link rel="stylesheet" href="../../css/CFNetPP_Base.css">
		<link rel="stylesheet" type="text/css" href="../../css/timecontrol.min.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/base.css"/>
		<script src="../../script/mui.min.js"></script>
		<script type="text/javascript" src="../../script/echarts.min.js"></script>
		<script src="../../script/jquery.min.js"></script>
		<script src="../../script/timecontrol.min.js"></script>
		<script src="../../script/common.js"></script>
		<script type="text/javascript" charset="utf-8">
			mui.init();
		</script>
		<style>
			.btns {
				background: #808A87;
				color: white;
			}
		</style>
	</head>
	<body >
		<div class="sign_top_div">
			<ul id="uidlist" class="mui-table-view top_10">
				<!--这里显示时间-->
				<li  class="mui-table-view-cell font" id="time"></li>
				<li  class="mui-table-view-cell mui-media">
					<div id='bMaplocate' class="mui-media-body">
						<img class="img" src="../../image/icon_dingwei.png">
						<span id="address" class="nowPos"></span>
					</div>
				</li>
			</ul>
			<button id="qiandao_bt" type="button" class=" top_30 bottom_30  mui-btn btns mui-btn-block  " onclick="onSignClick()" disabled="true">
				签到
			</button>
		</div>
		<ul id="res_list" class="mui-table-view msgdetail positions">
			<!--        <li dataid="'+data.id+'" class="mui-table-view-cell">
			<p class="mui-ellipsis"><span class="point"></span>2016-10-10 12:20  挖一会王挖煤你即开哦哦科学控温朗朗上口开始看我看卡拉斯你下面看没看</p>
			</li>-->
		</ul>
		<div class="noSign" hidden="hidden" id="noData">
			<img src="../../image/icon_img.png" width="60%">
			<p class="font16 t0p_10 base_dark_grey" >
				暂无签到数据！
			</p>
		</div>
		<div class="loadFailure" hidden="hidden" id="noNet">
			<img src="../../image/icon_img01.png" width="60%">
			<p class="" >
				数据加载失败
			</p>
			<p>
				请检查您的网络
			</p>
			<button type="button" class="mui-btn mui-btn-outlined" onclick="loadData()">
				重新加载
			</button>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/jquery.min.js"></script>
	<script type="text/javascript" src="../../script/MD5.js"></script>
	<script type="text/javascript" src="../../script/requestTool.js"></script>
	<script type="text/javascript" src="../../script/common.js"></script>
	<script type="text/javascript" src="../../script/databaseOperation.js"></script>
	<script type="text/javascript">
		var nowLat = 0.0;
		var nowLon = 0.0;
		var nowAddress = '';
		var page = 1;
		var time;
		var pid="";
		DBReady = function() {
			judgePracticeType();
			customUI();
			loadData();
			
			setupNowTime();
			getLocation(function(ret, err) {
				if (ret) {
					$('.nowPos').text(ret.address);
					nowAddress = ret.address;
				}
			});
		}
		//学生端获取实习计划状态（仅限学生端）
		function judgePracticeType() {
			var param = {
				uid : $api.getStorage('cfnetppuid'),
				jid : $api.getStorage('cfnetppjid')
			};
			cfnetppPOST(urlPracticeType, param, true, function(data, status) {
				if (status == 'success') {
					//															alert(JSON.stringify(data));
					//1,成功，返回success
					if (data.status == 1) {
						var list = data.json;
						if (list != null && list != '') {
							var sxtypes = parseInt(list.sxtypes);
								pid=list.pid;
							//0：顶岗，1：认知，2:跟岗
							if (sxtypes != 1) {
								$('#qiandao_bt').unbind('click').addClass('mui-btn-warning').removeClass('btns').attr('disabled', false);
							}
						} else {
							ShowToast('你还未有实习计划，请等待老师分配');
						}
					} else if (data.status == -1) {
						ShowToast('你还未有实习计划，请等待老师分配');
					} else {
						ShowToast('你还未有实习计划，请等待老师分配');
					}
				}
			})
		}

		function setupGps(callback) {
			var bMap = api.require('bMap');
			bMap.getLocationServices(function(ret, err) {
				if (ret.enable != true) {
					if (api.systemType == 'ios') {//iOS
						api.alert({
							title : 'GPS未开启',
							msg : '请于“设置”-->“隐私”-->“定位服务”，打开定位功能',
						}, function(ret, err) {
						});
					} else {//安卓
						api.alert({
							title : 'GPS未开启',
							msg : '请于系统设置中打开GPS定位功能',
						}, function(ret, err) {
						});
					}
				} else {//定位打开
					callback();
				}
			});
		}

		//设置ui
		function customUI() {
			//读取数据库最后一条签到记录
			var sql = "select signtime from t_sign_record  where uid='" + $api.getStorage('cfnetppuid') + "' order by id desc;";
			selectSQL(sql, function(ret, err) {
				if (ret.status) {
					var signArr = ret.data;
					if (signArr.length > 0) {
						var signData = ret.data[0];
						setupSignBtn(signData.signtime);
					} else {
						setupSignBtn(0);
					}
				} else {
					setupSignBtn(0);
				}
			});
		}

		function setupSignBtn(signtime) {
			if (signtime == 0) {//数据库没记录
				$('#qiandao_bt').click(function() {
					onSignClick();
				});
			} else {
				var nowTime = new Date().getTime();
				var oldTime = timeStampWithStr(signtime);
				if (getYYMMDD(nowTime) == getYYMMDD(oldTime)) {
					$('#qiandao_bt').unbind('click').addClass('btns').removeClass('mui-btn-warning').attr('disabled', "true");
				} else {
					$('#qiandao_bt').click(function() {
						onSignClick();
					});
				}
			}
		}

		function timeStampWithStr(timeStr) {
			if (timeStr.length == 10) {
				return parseInt(timeStr) * 1000;
			} else {
				return parseInt(timeStr);
			}
		}

		//点击签到
		function onSignClick() {
			if (nowLat == 0.0 || nowLon == 0.0) {
				setupGps(function() {
					api.showProgress({
						style : 'default',
						animationType : 'fade',
						title : '定位中...',
						text : '请稍等...',
						modal : true
					});
					getLocation(function(ret, err) {
						api.hideProgress();
						if (ret) {
							$('.nowPos').text(ret.address);
							nowAddress = ret.address;
							onSign();
						} else {
							api.toast({
								msg : '获取位置失败，请重试!'
							});
						}
					});
				});
			} else {
				onSign();
			}
		}

		//签到
		function onSign() {
			var timestamp = (Date.parse(new Date()) / 1000).toString();
			var params = {
				uid : $api.getStorage('cfnetppuid'),
				jid : $api.getStorage('cfnetppjid'),
				signtime : timestamp,
				lat : nowLat.toString(),
				lon : nowLon.toString(),
				address : nowAddress,
				pid:pid,
				message:'签到'
			};
			cfnetppPOST(urlSign, params, false, function(data, status) {
				SendBusinessOperationsLogNotifi('签到', '086', params, params, '', data, '签到接口', '086-01')
				if (status == 'success') {
					if (data.status == 1) {
						$('#qiandao_bt').unbind('click').addClass('btns').removeClass('mui-btn-warning').attr('disabled', "true");
						api.toast({
							msg : '签到成功',
							duration : 2000,
							location : 'bottom'
						});
						var dbData = {
							uid : $api.getStorage('cfnetppuid'),
							jid : $api.getStorage('cfnetppjid'),
							lat : nowLat,
							lon : nowLon,
							address : nowAddress,
							signtime : timestamp
						};
						insertData('t_sign_record', dbData, function(ret, err) {
							$('#noNet').hide();
							$('#noData').hide();
							$("#res_list").prepend(htmlStringWithName(dbData));
						});
					} else {
						////2,你还未被分配实习,返回job
						if (data.status == 2) {
							api.toast({
								msg : '你还未被分配实习,暂无法签到!',
								duration : 2000,
								location : 'bottom'
							});
							////3,您当前不在实习期内,返回time
						}
						else if (data.status == 3) {
							api.toast({
								msg : '您当前不在实习期内,无法签到!',
								duration : 2000,
								location : 'bottom'
							});
							////4,您今日已签到,返回has
						}
						else if (data.status == 4) {
							api.toast({
								msg : '您今日已签到!',
								duration : 2000,
								location : 'bottom'
							});
						} else if (data.status == 5) {
							api.toast({
								msg : '您已处于结业状态，无需签到!',
								duration : 2000,
								location : 'bottom'
							});
						} else {
							api.toast({
								msg : '签到失败,请重试!',
								duration : 2000,
								location : 'bottom'
							});
						}
					}
				} else {
					api.toast({
						msg : '签到失败,请重试!',
						duration : 2000,
						location : 'bottom'
					});
				}
			});
		}

		function loadData() {
			api.showProgress({
				style : 'default',
				animationType : 'fade',
				title : '数据加载中...',
				text : '请稍候...',
				modal : false
			});
			cfnetppPOST(urlSignlist, {
				uid : $api.getStorage('cfnetppuid'),
				jid : $api.getStorage('cfnetppjid'),
				all : 1
			}, true, function(data, status) {
				api.hideProgress();
				if (status == 'success') {
					if (data.status == 1) {
						var listArr = data.json;
						if (listArr != null && listArr.constructor == Array && listArr.length > 0) {
							$('#noNet').hide();
							$('#noData').hide();
							var htmlString = '';
							for (var i = 0; listArr.length > i; i++) {
								var data = listArr[i];
								htmlString += htmlStringWithName(data);
								if (i == 0) {
									var lastsignTime = data.signtime;
									setupSignBtn(lastsignTime);
								}
							}
							$("#res_list").html(htmlString);
						} else {
							$("#res_list").html('');
							$('#noNet').hide();
							$('#noData').show();
						}
					} else {
						$("#res_list").html('');
						$('#noNet').hide();
						$('#noData').show();
						// api.toast({
						//     msg: '暂无数据！',
						//     duration: 2000,
						//     location: 'bottom'
						// });
					}
				} else {
					$("#res_list").html('');
					$('#noData').hide();
					$('#noNet').show();
					// api.toast({
					//     msg: '获取签到列表失败！',
					//     duration: 2000,
					//     location: 'bottom'
					// });
				}
			});
		}

		function htmlStringWithName(data) {
			return '<li dataid="' + data.id + '" class="mui-table-view-cell">' + '<p ><span class="point"></span>' + getCurrentDate(data.signtime) + ' ' + data.address + '</p>' + '</li>';
		}

		function setupNowTime() {
			//更新时间
			function p(s) {
				return s < 10 ? '0' + s : s;
			}

			function showTime() {
				var date = new Date();
				var hour = date.getHours();
				var minute = date.getMinutes();
				var dateStr = p(hour) + ":" + p(minute);
				$('#time').html(dateStr);
			}

			var interval = window.setInterval(showTime, 1000);
		}

		//		function getNowFormatDate(time) {
		//			var datetime;
		//			if (time.length == 10) {
		//				datetime = parseInt(time) * 1000;
		//			} else {
		//				datetime = parseInt(time);
		//			}
		//			var date = new Date(datetime);
		//			var seperator1 = "-";
		//			var seperator2 = ":";
		//			var month = date.getMonth() + 1;
		//			var strDate = date.getDate();
		//			var mm = date.getMinutes();
		//			if (month >= 1 && month <= 9) {
		//				month = "0" + month;
		//			}
		//			if (strDate >= 0 && strDate <= 9) {
		//				strDate = "0" + strDate;
		//			}
		//			if (mm >= 0 && mm <= 9) {
		//				mm = "0" + mm;
		//			}
		//			var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + mm;
		//
		//			return currentdate;
		//		}
		function getNowFormatTime(date) {
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			var mm = date.getMinutes();
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			if (mm >= 0 && mm <= 9) {
				mm = "0" + mm;
			}
			var currentTime = date.getHours() + seperator2 + mm;
			return currentTime;
		}

		//获取百度地图!
		function getLocation(callback) {
			var bMap = api.require('bMap');
			bMap.getLocation({
				accuracy : '100',
				autoStop : true,
				filter : 1
			}, function(ret, err) {
				if (ret.status) {
					var lon = parseFloat(ret.lon);
					var lat = parseFloat(ret.lat);
					nowLat = lat;
					nowLon = lon;
					bMap.getNameFromCoords({
						lon : lon,
						lat : lat
					}, function(ret, err) {
						callback(ret, err);
					});
				} else {
					callback('', 'err');
				}
			});
		}

		function getLocationName(lon, lat) {
			var bMap = api.require('bMap');
			// alert('获取地理名称');
			bMap.getNameFromCoords({
				lon : lon,
				lat : lat
			}, function(ret, err) {
				//				 alert(JSON.stringify(ret) + '\n' + JSON.stringify(err));
				if (ret.status) {
					// alert(JSON.stringify(ret));
				} else {
					// alert(JSON.stringify(err));
				}
			});
		}

		function getYYMMDD(time) {
			var tempDate;
			if (time.length == 10) {
				tempDate = new Date(parseInt(time) * 1000);
			} else {
				tempDate = new Date(parseInt(time));
			}
			var y = tempDate.getFullYear();
			var m = tempDate.getMonth() + 1;
			var d = tempDate.getDate();
			return y + add0(m) + add0(d);
		}

		function add0(m) {
			return m < 10 ? '0' + m : m
		}
	</script>
</html>